function ajax(options) {
    let xhr = new XMLHttpRequest()

    // 设置默认请求方式
    options.type = options.type || 'GET'

    let arr = []
    for (let attr in options.data) {
        arr.push(attr + '=' + options.data[attr])
    }

    let str = arr.join('&')

    // 判断是否是get post
    if (options.type.toUpperCase() === 'GET') {
        xhr.open('GET', options.url + '?' + str, true)
        xhr.send()
    } else {
        xhr.open('POST', options.url, true)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send(str)
    }

    xhr.onreadystatechange = function () {
        if (xhr.status === 200) {
            if (xhr.readyState === 4) {
                options.success && options.success(xhr.responseText)
            }
        }
    }
}

function pAjax(options) {
    return new Promise(function (resolve, reject) {
        ajax({
            url: options.url,
            data: options.data,
            type: options.type,
            success (res) {
                resolve(res)
            }
        })
    })
}
